/*
 * Copyright 2023-2085 xujin <ceo@itgrail.com>
 *
 * Licensed under the Grail License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://itgrail.com/docs?id=3&catalogId=87
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.itgrail.pilot.infrastructure.adapters;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itgrail.grail.ddd.annotation.infrastructure.Adapter;
import com.itgrail.pilot.domain.user.ports.DingRobotPort;
import com.itgrail.pilot.infrastructure.tunnel.db.dao.DepartmentMapper;
import com.itgrail.pilot.infrastructure.tunnel.db.dao.DictDataMapper;
import com.itgrail.pilot.infrastructure.tunnel.db.dao.UserMapper;
import com.itgrail.pilot.infrastructure.tunnel.db.dataobject.DictDataDO;
import com.itgrail.pilot.infrastructure.tunnel.db.dataobject.UserDO;
import com.itgrail.pilot.infrastructure.tunnel.third.DingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Adapter
@Slf4j
public class DingRobotAdapter implements DingRobotPort {
    @Autowired
    private DictDataMapper dictDataMapper;


    @Autowired
    private DingService dingService;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DepartmentMapper deptMapper;

    @Override
    public void robotNotify() {

    }

    @Override
    public void robotTimeNotify(String time, String deptId, String content) {

    }

    /**
     * 获取部门机器人token
     * deptId   token  totalTime
     *
     * @return
     */
    private List<Map<String, String>> getDeptRobot() {
        List<Map<String, String>> list = new ArrayList<>();
        QueryWrapper<DictDataDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dict_code", "dept_robot");
        List<DictDataDO> dictDataDOS = dictDataMapper.selectList(queryWrapper);
        dictDataDOS.forEach(x -> {
            String itemValue = x.getItemValue();
            Map deptRobot = JSON.parseObject(itemValue, Map.class);
            list.add(deptRobot);
        });
        return list;
    }

    /**
     * 获取部门下的所有人
     *
     * @param dingDeptId
     * @return
     */
    private List<Integer> getDeptUsers(String dingDeptId) {
        //统计部门下的所有人
        List<Integer> list = new ArrayList<>();
        List<Integer> childDeptIds = getChildDept(dingDeptId, list);
        childDeptIds = childDeptIds.stream().distinct().collect(Collectors.toList());
        QueryWrapper<UserDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("dept_id", childDeptIds);
        List<UserDO> userDOS = userMapper.selectList(queryWrapper);
        return userDOS.stream().map(UserDO::getUserId).collect(Collectors.toList());
    }

    private List<Integer> getChildDept(String deptId, List<Integer> list) {
        List<Integer> childDeptIds = dingService.getChildDeptIds(Integer.valueOf(deptId));
        if (!CollectionUtils.isEmpty(childDeptIds)) {
            childDeptIds.forEach(x -> {
                List<Integer> childDept = getChildDept(x.toString(), list);
                list.add(x);
                list.addAll(childDept);
            });
        }
        return list;
    }

    private List<String> userIdToDingUserId(List<Integer> userIds) {
        if (CollectionUtils.isEmpty(userIds)) {
            return Collections.emptyList();
        }
        QueryWrapper<UserDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id", userIds);
        List<UserDO> userDOS = userMapper.selectList(queryWrapper);
        return userDOS.stream().map(UserDO::getMobile).collect(Collectors.toList());
    }

    private List<Integer> getWhiteList() {
        QueryWrapper<DictDataDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("item_code", "whiteList");
        DictDataDO dictDataDO = dictDataMapper.selectOne(queryWrapper);
        String itemValue = dictDataDO.getItemValue();
        return JSON.parseArray(itemValue, Integer.class);
    }
}
